//
//  UIBarButtonItem+Extension.swift
//  Sinaweibotzw
//
//  Created by 檀志文 on 16/4/13.
//  Copyright © 2016年 ios檀志文. All rights reserved.
//

import UIKit

extension UIBarButtonItem{
//
//    2 便利构造函数最终需要调用一个指定的构造函数
//    调用便利构造函数，要保证调用这个便利构造函数会调用到指定的构造函数

    convenience init(imageName: String, target: AnyObject?, action: Selector) {
        let button = UIButton()
        // 设置按钮
    button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
    button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        
        button.sizeToFit()
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        self.init(customView: button)
    }
//    使用类方法
    class func createBarButtonItem(imageName: String) -> UIBarButtonItem {
//        创建按钮
        let button = UIButton()
//        设置按钮
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        
        // 创建UIBarButtonItem设置customView,返回回去
        return UIBarButtonItem(customView: button)
    }
}
